﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Hypervisor管理工具
{
    public partial class Form1 : Form
    {
        // 实例化HypervisorController用于管理Hypervisor的状态
        private HypervisorController _controller;

        public Form1()
        {
            InitializeComponent();
            _controller = new HypervisorController();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            UpdateStatusLabel();
        }

        /// <summary>
        /// 更新状态标签以显示当前的Hypervisor状态。
        /// </summary>
        private void UpdateStatusLabel()
        {
            try
            {
                string status = _controller.GetCurrentHypervisorStatus();
                lblStatus.Text = "当前状态: " + status;
            }
            catch (Exception ex)
            {
                MessageBox.Show($"{ex.Message}", "错误",
                    MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        // 当点击“启用自动”按钮时调用，设置Hypervisor为自动加载模式
        private void btnEnableAuto_Click(object sender, EventArgs e)
        {
            try
            {
                _controller.EnableAuto();
                UpdateStatusLabel();

                DialogResult dialogResult = MessageBox.Show("修改后重启才会生效，是否重启？", "提示",
                    MessageBoxButtons.YesNo, MessageBoxIcon.Information);

                if(dialogResult == DialogResult.Yes)
                {
                    SystemShutdownUtil.Reboot();
                }
            }
            catch(Exception ex)
            {
                MessageBox.Show($"{ex.Message}", "错误",
                    MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        // 当点击“禁用”按钮时调用，禁用Hypervisor
        private void btnDisable_Click(object sender, EventArgs e)
        {
            try
            {
                _controller.Disable();
                UpdateStatusLabel();

                DialogResult dialogResult = MessageBox.Show("修改后重启才会生效，是否重启？", "提示",
                    MessageBoxButtons.YesNo, MessageBoxIcon.Information);

                if (dialogResult == DialogResult.Yes)
                {
                    SystemShutdownUtil.Reboot();
                }
            }
            catch(Exception ex)
            {
                MessageBox.Show($"{ex.Message}", "错误",
                    MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}
